#!/usr/bin/python

"""Project Euler Solution 033

Copyright (c) 2011 by Robert Vella - robert.r.h.vella@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import cProfile
from euler.numbers.advanced_math import true_product
from fractions import Fraction
from euler.numbers.decimal_base import integer_to_digits

def get_answer():
    def iscurious_fraction(n, d):
        """Returns true if the fraction [n]/[d] satisfies the properties 
        specified by this problem for a curious fraction. That is, a 
        fraction which can be simplified to the same fraction that one
        would get if a common digit between the denominator and the 
        numerator were removed. 
        
        Note:
            - If the common digit is 0, the fraction is considered trivial
            and this function returns false.
        """
        
        #If the numerator is equal to the denominator, then both digits
        #are common in both numbers, and therefore cannot be tested.
        if n == d:
            return False    
        
        #If both numbers are divisable by 11, then they certainly don't have
        #a common digit unless they are equal. As specified above, equal
        #numbers are not tested. 
        if n % 11 == d % 11 == 0:
            return False    
        
        
        #If both numbers are divisable by 10, then they are considered trivial.
        #If only one of the numbers is divisable by 10, then they certainly
        #don't satisfy the requirements for this function, as a number which
        #"starts" with 0 only has one digit. 
        if n % 10 == 0 or d % 10 == 0:
            return False    
    
        #Get the common digit between the numerator and the denominator.
        numerator_digits = list(integer_to_digits(n))
        denominator_digits = list(integer_to_digits(d))
        
        common_digit_set = set(numerator_digits)\
                            .intersection(set(denominator_digits))
        
        if len(common_digit_set) == 0:
            return False
        
        common_digit = list(common_digit_set)[0]
        
        #Get the remaining digit when the common digit is removed.
        numerator_digits.remove(common_digit)    
        denominator_digits.remove(common_digit)    
        
        remaining_numerator_digit = float(list(numerator_digits)[0]) 
        remaining_denominator_digit = float(list(denominator_digits)[0])
        
        #Return true if the numbers divided by their remaining digit give
        #the same factor.
        return n / remaining_numerator_digit == d / remaining_denominator_digit

    #Return result
    return true_product(
                       Fraction(n, d) for d in xrange(12, 99) 
                        for n in xrange(12, d)
                        if iscurious_fraction(n, d)
                    ).denominator
    

if __name__ == "__main__":
    cProfile.run("print(get_answer())")
